package com.huluobo.higt4demo1911.utils.gift.impl;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.huluobo.higt4demo1911.R;

/**
 * 礼物的数字view
 * Created by lc on 2022/5/6.
 */
public class NumView extends LinearLayout {
    public NumView(Context context) {
        super(context);
        init();
    }

    public NumView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {

    }

    //当前礼物的数量
    private int currentNum;

    public int getCurrentNum() {
        return currentNum;
    }

    /**
     * 展示数量文字 复用+缓存
     */
    public void showNum(int num) {
        removeAllViews();
        currentNum = num;
        if (num == 0) {
            return;
        }

        //获取数字,并且进行循环,依次添加view
        char[] chars = String.valueOf(num).toCharArray();
        for (char c : chars) {
            addView(getNumView(c));
        }
    }

    public void showNumAdd() {
        showNum(++currentNum);
    }

    /**
     * 单个文字的View
     */
    private ImageView getNumView(char c) {
        ImageView numIv = new ImageView(getContext());
        switch (c) {
            default:
            case '0':
                numIv.setImageResource(R.mipmap.ca0);
                return numIv;
            case '1':
                numIv.setImageResource(R.mipmap.ca1);
                return numIv;
            case '2':
                numIv.setImageResource(R.mipmap.ca2);
                return numIv;
            case '3':
                numIv.setImageResource(R.mipmap.ca3);
                return numIv;
            case '4':
                numIv.setImageResource(R.mipmap.ca4);
                return numIv;
            case '5':
                numIv.setImageResource(R.mipmap.ca5);
                return numIv;
            case '6':
                numIv.setImageResource(R.mipmap.ca6);
                return numIv;
            case '7':
                numIv.setImageResource(R.mipmap.ca7);
                return numIv;
            case '8':
                numIv.setImageResource(R.mipmap.ca8);
                return numIv;
            case '9':
                numIv.setImageResource(R.mipmap.ca9);
                return numIv;
        }
    }
}
